<?php
// +----------------------------------------------------------------------
// | OneThink [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.onethink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: 麦当苗儿 <zuojiazi@vip.qq.com> <http://www.zjzit.cn>
// +----------------------------------------------------------------------

namespace Home\Controller;

/**
 * 前台首页控制器
 * @author 麦当苗儿 <zuojiazi@vip.qq.com>
 */
class HomePageController extends HomeController {

    /**
     * 后台首页
     * @author 麦当苗儿 <zuojiazi@vip.qq.com>
     */
    public function index(){
        
        $imgdata = $this->homePageImgGet();
        foreach ($imgdata as $key => $value) {
            if($value){
                $save_img_data[] = $value;
            }
        }
        if(empty($imgdata)){
        	$save_img_data = null;
        } 

        $json['banners'] = $save_img_data;

        $articleshowdata = $this->articleShowGet(-1,0,1,10);

        $json['articleshow'] = $articleshowdata;

        $this->ajaxReturn($json);
    }

    public function homePageImgGet(){
        $homepageimgdata = $this->getHomePageImg();
        $imgdata = $this->imgIdToPicture($homepageimgdata);
        return $imgdata;
    }

    protected function getHomePageImg(){
        $homepageimg = D('homepageimg');
        $homepageimgdata = $homepageimg->homePageImgGet();
        return $homepageimgdata;
    }

    protected function imgIdToPicture($homepageimgdata){
        if($homepageimgdata){
            $picture = D('picture');
            $imgdata = $picture->getPictureByIds($homepageimgdata);
        }else{
            $imgdata = null;
        }
        
        return $imgdata;
    }

    public function articleShowGet($id,$type,$page,$limit){
        $uid = $this->login();
        $articleshowdata = $this->getArticleShow($id,$type,$page,$limit);
        $collectiondata = $this->collectionGet($uid);
        if($collectiondata['success']){
            $collectionArray = $collectiondata['info'];
        }else{
            $collectionArray = array();
        }
        foreach ($articleshowdata as $key => $value) {
            if(in_array($value['id'], $collectionArray)){
                $articleshowdata[$key]['collection'] = true;
            }else{
                $articleshowdata[$key]['collection'] = false;
            }
        }
        foreach ($articleshowdata as $key => $value) {
             $save_data[$key] = array(
            'id' => $value['id'], 
            'title'=>$value['title'], 
            'content' => $value['content'], 
            'description'=>$value['description'], 
            'recommend' => (bool)$value['recommend'], 
            'hands' => (bool)$value['hands'], 
            'party' => (bool)$value['party'], 
            'super' => (bool)$value['super'], 
            'green' => (bool)$value['green'], 
            'buffet' => (bool)$value['buffet'], 
            // 'high_price'=>$value['high_price'], 
            // 'low_price' => $value['low_price'], 
            'one_price' =>floatval($value['one_price']),
            'two_price' => floatval($value['two_price']),
            'three_price'=>floatval($value['three_price']),
            'four_price' =>floatval($value['four_price']),
            'collect_num'=>$value['collect_num'], 
            'collection' =>$value['collection'],
            'epense_explanation'=>$value['epense_explanation'],
            );
             $banners = array();
             $banners =array(
                '0' => $this->imgIdToPicture($articleshowdata[$key]['banner1']), 
                '1' => $this->imgIdToPicture($articleshowdata[$key]['banner2']), 
                '2' => $this->imgIdToPicture($articleshowdata[$key]['banner3']), 
                '3' => $this->imgIdToPicture($articleshowdata[$key]['banner4']), 
                '4' => $this->imgIdToPicture($articleshowdata[$key]['banner5']), 
                );
            $save_banners_data =array();
            foreach ($banners as $key2 => $value2) {
                if($value2){
                    $save_banners_data[] = $value2;  
                }
                
            }

            if($save_banners_data){
                $save_data[$key]['banners'] = $save_banners_data;
            }else{
                $save_data[$key]['banners'] = null;
            }

        }

        return $save_data;
    }

    protected function getArticleShow($id,$type,$page,$limit){
        $articleshow = D('articleshow');
        $articleshowdata = $articleshow ->articleShowGet($id,$type,$page,$limit);
        
        return $articleshowdata;
    }

    protected function collectionGet($uid){
        $collection = D('collection');
        $collectiondata = $collection->collectionGet($uid);
        return $collectiondata;
    }

}
